//
//  CJCustomGoodsTypeAlertView.m
//  DatangLife
//
//  Created by 陈建 on 2018/5/7.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "CJCustomGoodsTypeAlertView.h"

#import "CJCustomChoosTypeTableViewCell.h"
#import "CJCustomGoodsTypeModel.h"
#import "CJCustomGoodsInfoView.h"
#import "CJCustomCountView.h"

@implementation CJCustomGoodsTypeAlertView
{
    UIButton *sureButton;
    UIView *backView;//灰色view
    UIView *contentView;//白底View
    CJCustomSizeAttributeModel *sizeModel;
    CJCustomGoodsInfoView *infoView;
    CJCustomCountView *countView;
    NSString *_selectedSring;
}

- (instancetype)initWithFrame:(CGRect)frame andHeight:(CGFloat)height{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kClearColor;
        
        backView = [[UIView alloc] initWithFrame:self.bounds];
        backView.backgroundColor = kRGBAColor(0, 0, 0, 0.4);
        [self addSubview:backView];
        WeakSelf(weakSelf);
        [backView setTapActionWithBlock:^{
            [weakSelf hideView];
        }];
        
        //白底view
        contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, height)];

        contentView.userInteractionEnabled = YES;
        [self addSubview:contentView];
        
        //商品信息
        infoView = [[CJCustomGoodsInfoView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kSize(110))];
        [contentView addSubview:infoView];
        
        sureButton = [CJUIKit buttonWithBackgroundColor:kCommonRedColor titleColorForNormal:kWhiteColor titleForNormal:@"确定" titleForSelete:nil titleColorForSelete:nil fontSize:18 font:nil];
        [sureButton addTarget:self action:@selector(sureBtnClcik) forControlEvents:UIControlEventTouchUpInside];
        
        [contentView addSubview:sureButton];
        
        [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(contentView);
            make.height.equalTo(@49);
        }];
        [contentView addSubview:self.tableview];
        [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(contentView);
            make.top.equalTo(infoView.mas_bottom);
            make.bottom.equalTo(sureButton.mas_top);
        }];
    }
    return self;
}

- (void)sureBtnClcik{
    for (CJCustomGoodsTypeModel *model in _dataSource) {
        
        if (model.selectIndex < 0) {
            [CJUIKit showErrorWithStatus:[NSString stringWithFormat:@"请选择%@",model.typeName]];
            return;
        }
    }
//    if (_dataSource.count == 0) {
//        //该商品无规格
//        [CJUIKit showErrorWithStatus:@"该商品无规格"];
//        [self hideView];
//        return;
//    }
    if (self.selectedSize) {
        self.selectedSize(_selectedSring, countView.countTextField.text);
    }
    [self hideView];
    //判断库存
//    if ([sizeModel.stock integerValue] > 0) {
//        if (self.selectSize) {
//            sizeModel.count = countView.countTextField.text;
//            self.selectSize(sizeModel);
//
//        }
//        [self hideView];
//    }
//    else{
//        [CJUIKit showErrorWithStatus:@"该规格商品暂无库存,无法加入购物车"];
//    }
}

- (void)showView{
    self.alpha = 1;
    [UIView animateWithDuration:0.25 animations:^{
        contentView.centerY = contentView.centerY - CGRectGetHeight(contentView.frame);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hideView{
    [self tfresignfirstResponder];
    [UIView animateWithDuration:0.25 animations:^{
        contentView.centerY = contentView.centerY + CGRectGetHeight(contentView.frame);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)addBtnClick{
    NSInteger count = [countView.countTextField.text integerValue];
    if (sizeModel) {
        if (count < [sizeModel.stock integerValue]) {
            countView.countTextField.text = [NSString stringWithFormat:@"%ld",count + 1];
        }
    }else{
//        if (count < [_model.totalStock integerValue]) {
//            countView.countTextField.text = [NSString stringWithFormat:@"%ld",count + 1];
//
//        }
        if (count < 99) {
            countView.countTextField.text = [NSString stringWithFormat:@"%ld",count + 1];
            
        }
    }
}

- (void)reduceBtnClick{
    NSInteger count = [countView.countTextField.text integerValue];
    if (count > 1) {
        countView.countTextField.text = [NSString stringWithFormat:@"%ld",count - 1];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"CJCustomChoosTypeTableViewCell";
    CJCustomChoosTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[CJCustomChoosTypeTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    CJCustomGoodsTypeModel *model = _dataSource[indexPath.row];
    tableView.rowHeight = [cell setData:model];
    WeakSelf(weakSelf);
    cell.selectButton = ^(NSInteger selectIndex) {
        [weakSelf reloadGoodsInfo];
    };
    return cell;
}

- (void)initData:(CJCustomGoodsModel *)model{
    _model = model;
    [infoView initData:model];
    [_dataSource removeAllObjects];
    //传入数据源创建多个属性
    [_dataSource addObjectsFromArray:model.itemsList];
    [self reloadGoodsInfo];
    [self.tableview reloadData];
}

- (void)reloadGoodsInfo{
    for (CJCustomGoodsTypeModel *model in _dataSource) {
        if (model.selectIndex < 0) {
            infoView.promatLabel.text = [NSString stringWithFormat:@"请选择%@",model.typeName];
            break;
        }
    }
    NSString *str = [self getSizeStr];
    _selectedSring = str;
    NSLog(@"%@",str);
    /*
    sizeModel = nil;
    
    for (CJCustomSizeAttributeModel *model in _model.sizeAttribute) {
        if ([model.value isEqualToString:str]) {
            sizeModel = model;
            if ([countView.countTextField.text integerValue] > [sizeModel.stock integerValue]) {
                countView.countTextField.text = [NSString stringWithFormat:@"%ld",[sizeModel.stock integerValue]];
            }else if ([countView.countTextField.text integerValue] < [sizeModel.stock integerValue]){
                if ([countView.countTextField.text integerValue] == 0) {
                    countView.countTextField.text = @"1";
                }
            }
            infoView.promatLabel.text = [NSString stringWithFormat:@"已选%@",model.value];
            [infoView resetData:model];
            return;
        }
    }
    
    
    
    [infoView initData:_model];
     */
}


- (void)tfresignfirstResponder{
    self.tableview.contentOffset = CGPointMake(0, 0);
}



-(UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableview.sectionHeaderHeight = 0;
        _tableview.backgroundColor = kWhiteColor;
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _dataSource = [[NSMutableArray alloc] init];
        countView = [[CJCustomCountView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kSize(50))];
        
        [countView.addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [countView.reduceBtn addTarget:self action:@selector(reduceBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _tableview.tableFooterView = countView;
        
        
    }
    return _tableview;
}

-(NSString *)getSizeStr
{
    //拼接属性字符串
    NSString *str=@"";
    for (CJCustomGoodsTypeModel *model in _dataSource) {
        if (model.selectIndex>=0) {
            if (str.length == 0) {
                str = model.typeArray[model.selectIndex];
            }else
                str = [NSString stringWithFormat:@"%@、%@",str,model.typeArray[model.selectIndex]];
        }
    }
    return str;
}


-(void)dealloc
{
    NSLog(@"dddddd");
}

@end
